package org.batatasframework.web.element;

import org.batatasframework.web.HtmlRenderHelper;
import org.batatasframework.web.template.TemplateRender;
import org.springframework.web.util.HtmlUtils;

public class Input extends AbstractElement{
	private final String type;
	private String value;

	private Input(String type){
		super("input");
		this.type = type;
	}
	
	public Input(String type, HtmlAttribute attr, TemplateRender templateRender, String propretyName){
		this(type);
		this.attribute = attr;
	
		setValue(templateRender, propretyName);
	}

	public Input(String type, HtmlAttribute attr, String value){
		this(type);
		this.attribute = attr;
		
		setValue(value);
	}
	
	public void setValue(TemplateRender templateRender, String propretyName){
		this.value = templateRender.valueEscape(propretyName, true);
	}
	
	public void setValue(String value){
		this.value = HtmlUtils.htmlEscape(value);
	}
	
	@Override
	protected void readAttribute(StringBuilder html) {
		HtmlRenderHelper.attribute(html, "type", type, false);
		super.readAttribute(html);
	}
	
	@Override
	protected void elementClosed(StringBuilder html) {
		HtmlRenderHelper.attribute(html, "value", value, true);
		super.elementClosed(html);
	}
	
	//-------------------------------------
	
	public Input button(){
		return new Input("button");
	}
	
	public Input checkbox(){
		return new Input("checkbox");
	}
	
	public Input file(){
		return new Input("file");
	}
	
	public Input hidden(){
		return new Input("hidden");
	}
	
	public Input image(){
		return new Input("image");
	}
	
	public Input password(){
		return new Input("password");
	}
	
	public Input radio(){
		return new Input("radio");
	}
	
	public Input reset(){
		return new Input("reset");
	}
	
	public Input submit(){
		return new Input("submit");
	}
	
	public Input text(){
		return new Input("text");
	}
}
